package com.jlnk.androidfmc.fragment.fragments.homefragment;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.jlnk.androidfmc.FourthActivity;
import com.jlnk.androidfmc.R;
import com.jlnk.androidfmc.adapter.ProductAdapter;
import com.jlnk.androidfmc.entity.Product;
import com.jlnk.androidfmc.presenter.RecommendPresenter;
import com.jlnk.androidfmc.view.RecommendView;

import java.util.ArrayList;
import java.util.List;


public class RecommendFragment extends Fragment implements RecommendView, ProductAdapter.OnProductClickListener {
    private RecommendPresenter presenter;
    private RecyclerView recyclerView;
    private ProductAdapter adapter;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_recommend, container, false);

        // 初始化 RecyclerView
        recyclerView = view.findViewById(R.id.recyclerViewProducts);
        recyclerView.setLayoutManager(new GridLayoutManager(requireContext(), 2));

        presenter = new RecommendPresenter(this, requireActivity());
        presenter.loadProducts();

        // 初始化 adapter 并设置点击事件监听器
        adapter = new ProductAdapter(new ArrayList<>());
        adapter.setOnProductClickListener(this);
        recyclerView.setAdapter(adapter);

        return view;
    }

    @Override
    public void showProducts(List<Product> productList) {
        // 更新 adapter 数据
        adapter.setProductList(productList);
        adapter.notifyDataSetChanged();
    }

    // 实现 OnProductClickListener 接口的方法
    @Override
    public void onProductClick(View view, Product product) {
        // 当某个产品项被点击时，此方法会被调用
        Intent intent = new Intent(view.getContext(), FourthActivity.class);
        // 将产品信息作为额外数据传递给 FourthActivity
        intent.putExtra("PRODUCT", product); // 假设您已经在 FourthActivity 中定义了接收 PRODUCT 额外数据的逻辑
        view.getContext().startActivity(intent);
    }

    @Override
    public void showLoading() {
        // 显示加载动画或信息，具体实现根据需要
    }

    @Override
    public void hideLoading() {
        // 隐藏加载动画或信息，具体实现根据需要
    }

    @Override
    public void showError() {
        // 显示错误信息，具体实现根据需要
    }
}